package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.DaoRewardsPropsDao;
import com.sc.nft.dao.UserCreatorInfoDao;
import com.sc.nft.entity.DaoRewardsProps;
import com.sc.nft.entity.EquityProps;
import com.sc.nft.entity.vo.DaoRewardsPropsVO;
import com.sc.nft.entity.vo.GetDaoByIdVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.DaoRewardsPropsService;
import com.sc.nft.service.EquityPropsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class DaoRewardsPropsServiceImpl implements DaoRewardsPropsService {

    private final DaoRewardsPropsDao daoRewardsPropsDao;
    private final EquityPropsService equityPropsService;
    private final UserCreatorInfoDao userCreatorInfoDao;

    @Value("${enableRewardConsumption.daoId:162}")
    private Long PROPS_RELEASED_DAO_ID;


    @Override
    public Boolean saveBatch(List<Long> propsList) {
        //判重
        LambdaQueryWrapper<DaoRewardsProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(DaoRewardsProps::getPropsId, propsList);
        wrapper.eq(DaoRewardsProps::getDaoId, PROPS_RELEASED_DAO_ID);
        List<DaoRewardsProps> list = daoRewardsPropsDao.list(wrapper);

        if (CollectionUtil.isNotEmpty(list)) {
            String propsName = list.stream()
                    .map(daoRewardsProps -> {
                        EquityProps equityProps = equityPropsService.getById(daoRewardsProps.getPropsId());
                        return equityProps.getFirstTitle();
                    })
                    .collect(Collectors.joining("、"));

            throw new GlobalRunTimeException("道具【" + propsName + "】已存在，不能重复添加");
        }

        List<DaoRewardsProps> daoRewardsPropsList = new ArrayList<>();
        for (Long propsId : propsList) {
            DaoRewardsProps daoRewardsProps = new DaoRewardsProps();
            daoRewardsProps.setPropsId(propsId);
            daoRewardsProps.setDaoId(PROPS_RELEASED_DAO_ID);
            daoRewardsPropsList.add(daoRewardsProps);
        }
        return daoRewardsPropsDao.saveBatch(daoRewardsPropsList);
    }

    @Override
    public Boolean remove(Long id) {
        return daoRewardsPropsDao.removeById(id);
    }

    @Override
    public Page<DaoRewardsPropsVO> page(Page page) {
        // 构建查询条件
        LambdaQueryWrapper<DaoRewardsProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(DaoRewardsProps::getCreateTime);

        // 执行分页查询
        Page<DaoRewardsProps> entityPage = daoRewardsPropsDao.page(page, wrapper);

        // 转换为VO
        Page<DaoRewardsPropsVO> voPage = new Page<>(entityPage.getCurrent(), entityPage.getSize(), entityPage.getTotal());
        voPage.setRecords(entityPage.getRecords().stream().map(this::convertToVO).collect(Collectors.toList()));

        return voPage;
    }

    private DaoRewardsPropsVO convertToVO(DaoRewardsProps entity) {
        DaoRewardsPropsVO vo = new DaoRewardsPropsVO();
        BeanUtils.copyProperties(entity, vo);
        EquityProps equityProps = equityPropsService.getById(entity.getPropsId());
        vo.setPropsName(equityProps.getFirstTitle());
        vo.setCreationContent(equityProps.getCreationContent());

        GetDaoByIdVO getDaoByIdVO = userCreatorInfoDao.getDaoById(entity.getDaoId());
        if (Objects.nonNull(getDaoByIdVO)) {
            vo.setDaoName(getDaoByIdVO.getDaoName());
        }
        return vo;
    }
}
